package Stack;

import java.util.Arrays;

public class MyStack<I extends Number> {
    private int[] elem;
    private int usedSize;
    private static final int DEFULT_SIZE=10;

    public MyStack(){
        elem=new int[DEFULT_SIZE];
    }
    //存储数组
    public void push(int val){
        if(isFull()){
            grow();
        }
        elem[usedSize]=val;
        usedSize++;
    }

    private void grow(){
        elem= Arrays.copyOf(elem,elem.length);
    }

    public boolean isFull(){
        return usedSize==elem.length;
    }

    //删除数据
    public int pop(){
        if(isEmpty()){
            throw new StackeEmptyExcption("栈为空异常");
        }
        usedSize--;
        return elem[usedSize];
    }

    public boolean isEmpty(){
        return usedSize==0;
    }


    public int peek(){
        if(isEmpty()){
            throw new StackeEmptyExcption("栈为空异常");
        }
        return elem[usedSize];
    }


    public int size(){
        return usedSize;
    }

}
